<template>
	<view class="login-container">
		<view class="login-content">
			<view class="login-header">
				<image class="hospital-logo" src="/static/hospital-logo.png" mode="aspectFit"></image>
				<text class="hospital-name">智慧医疗健康平台</text>
				<text class="welcome-text">欢迎登录</text>
			</view>

			<view class="login-form">
				<view class="form-item">
					<image class="input-icon" src="/static/icon-user.png" mode="aspectFit"></image>
					<input class="form-input" type="text" v-model="username" placeholder="请输入用户名/手机号"
						placeholder-class="input-placeholder" />
				</view>

				<view class="form-item">
					<image class="input-icon" src="/static/icon-password.png" mode="aspectFit"></image>
					<input class="form-input" type="password" v-model="password" placeholder="请输入密码"
						placeholder-class="input-placeholder" />
				</view>

				<view class="form-item captcha-item">
					<image class="input-icon" src="/static/icon-captcha.png" mode="aspectFit"></image>
					<input class="form-input captcha-input" type="text" v-model="code" placeholder="请输入验证码"
						placeholder-class="input-placeholder" />
					<image class="captcha-image" :src='`data:image/png;base64,${imageBade64}`' mode="aspectFit"
						@click="getCodeHandler"></image>
				</view>

				<button class="login-button" type="primary" @tap="handleLogin">登 录</button>
			</view>

			<view class="login-footer">
				<text class="footer-link" @tap="gotoQuickLogin">快捷登录</text>
				<text class="footer-link" @tap="gotoForget">忘记密码?</text>
				<text class="footer-link" @tap="gotoRegister">注册账号</text>
				<text class="footer-copyright">©2025 智慧医疗健康平台</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		login,
		getCode
	} from '@/api/user.js'
	import token from '../../utils/token';

	export default {
		data() {
			return {
				username: '123',
				password: '123456',
				code: '',
				codeKey: '',
				imageBade64: '',
				oldCode: ''
			}
		},
		onLoad() {
			this.getCodeHandler();
		},
		methods: {
			//验证码
			async getCodeHandler() {
				try {
					const res = await getCode({
						userUnique: this.oldCode
					});
					if (res.code === 1) {
						this.codeKey = res.data.unique,
							this.imageBade64 = res.data.code
					}
					this.oldCode = this.codeKey
				} catch (e) {
					console.error('获取验证码失败', e);
					uni.showToast({
						title: '获取验证码失败',
						icon: 'none'
					});
				}
			},
			//登录
			async handleLogin() {
				if (!this.username || !this.password) {
					uni.showToast({
						title: '请输入用户名和密码',
						icon: 'none'
					});
					return;
				}

				if (!this.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}

				try {
					const res = await login({
						account: this.username,
						password: this.password,
						code: this.code,
						codeKey: this.codeKey
					});

					if (res.code === 1) {
						// 显示加载中提示
						uni.showLoading({
							title: '登录中...',
							mask: true
						});

						// 模拟加载延迟（如果需要）
						await new Promise(resolve => setTimeout(resolve, 1000));

						// 存储登录状态和token
						uni.setStorageSync('isLogin', true);
						token.setToken(res.data);

						// 隐藏加载提示
						uni.hideLoading();

						// 显示成功提示
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						});

						// 跳转到首页
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}, 1500);
					} else if (res.code === 2) {
						uni.showToast({
							title: res.msg,
							icon: 'error'
						});
					} else if (res.code === 3) {
						uni.showToast({
							title: res.msg,
							icon: 'error'
						});
					} else {
						uni.showToast({
							title: '登录失败',
							icon: 'error'
						});
					}
				} catch (e) {
					console.error('登录失败', e);
				}
				this.getCodeHandler();
				this.code = '';
			},
			//手机验证码登录
			gotoQuickLogin() {
				uni.navigateTo({
					url: '/pages/login/phone-login'
				});
			},
			//跳转到注册
			gotoRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				});
			},
			//忘记密码
			gotoForget() {
				uni.navigateTo({
					url: '/pages/forget/forget'
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		overflow: hidden;
	}

	.login-container {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: linear-gradient(to bottom, #f0f9ff, #ffffff);
		padding: 0 50rpx;
		box-sizing: border-box;
		overflow: hidden;
	}

	.login-content {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 80rpx 0;
		height: calc(100vh - 160rpx);
	}

	.login-header {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 60rpx;
	}

	.hospital-logo {
		width: 120rpx;
		height: 120rpx;
		margin-bottom: 20rpx;
	}

	.hospital-name {
		font-size: 36rpx;
		font-weight: bold;
		color: #1a73e8;
		margin-bottom: 10rpx;
	}

	.welcome-text {
		font-size: 28rpx;
		color: #666;
	}

	.login-form {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.form-item {
		position: relative;
		margin-bottom: 40rpx;
		border-bottom: 1rpx solid #e0e0e0;
		padding-bottom: 20rpx;
		display: flex;
		align-items: center;

		&.captcha-item {
			padding-bottom: 0;
			border-bottom: none;
		}
	}

	.input-icon {
		width: 40rpx;
		height: 40rpx;
		margin-right: 20rpx;
	}

	.form-input {
		flex: 1;
		height: 60rpx;
		font-size: 30rpx;
		color: #333;

		&.captcha-input {
			flex: none;
			width: 300rpx;
		}
	}

	.input-placeholder {
		color: #bbb;
		font-size: 28rpx;
	}

	.captcha-image {
		width: 200rpx;
		height: 80rpx;
		margin-left: 20rpx;
		border-radius: 8rpx;
		border: 1rpx solid #e0e0e0;
	}

	.login-button {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 45rpx;
		background: linear-gradient(to right, #1a73e8, #4a90e2);
		color: #fff;
		font-size: 32rpx;
		margin-top: 60rpx;
		box-shadow: 0 10rpx 20rpx rgba(26, 115, 232, 0.2);
	}

	.login-footer {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 40rpx;
	}

	.footer-link {
		font-size: 28rpx;
		color: #1a73e8;
		margin-bottom: 20rpx;
	}

	.footer-copyright {
		font-size: 24rpx;
		color: #999;
		margin-top: 20rpx;
	}
</style>